package de.recipeminer.ui.commands.workflow;

import de.recipeminer.mining.TermFrequencyImpl;
import de.recipeminer.tools.LimitablePrintStream;
import de.recipeminer.ui.commands.UserCommand;
import de.recipeminer.ui.parsing.OptionParser;
import joptsimple.OptionSet;
import org.apache.log4j.Logger;
import de.recipeminer.tools.Config;

import static java.util.Arrays.asList;

/**
 * @autor: Markus Ackermann
 */
public class ReadStopWordsCommand extends UserCommand.WorkflowCommand {
    private static Logger logger = Logger.getLogger(ReadStopWordsCommand.class);
    public static final String NAME = "read_stopwords";

    @Override
    public boolean run(OptionSet options, LimitablePrintStream out) {

        String fileToRead = Config.STOPWORD_FILE_LOCATION;
        if (options.has( "input") ) {
            fileToRead = (String) options.valueOf("input");
        }
        TermFrequencyImpl timpl = TermFrequencyImpl.getInstance();
        
        int sizebefore = timpl.getSizeOfActualStopwordList();
        timpl.updateStopWordSetFromFile(fileToRead);

        out.print("\n" + timpl.getSizeOfActualStopwordList() + " stopwords now.\n");
        return true;
    }

    @Override
    public String commandName() {
        return NAME;
    }

    @Override
    public void extendOptions(OptionParser parser) {
        parser.accepts("input", "Path to file to read in")
                .withOptionalArg().withValuesConvertedBy(OptionParser.REGEXP_CONVERTER)
                .describedAs("file").defaultsTo(OptionParser.REGEXP_CONVERTER.convert(Config.STOPWORD_FILE_LOCATION));
    }
}
